package com.tyg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tyg.entity.RoleInfo;
import com.tyg.entity.UserInfo;
import com.tyg.mapper.RoleInfoMapper;
import com.tyg.service.RoleInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-10-22
 */
@Service
@Transactional
public class RoleInfoServiceImpl extends ServiceImpl<RoleInfoMapper, RoleInfo> implements RoleInfoService {

    @Autowired
    RoleInfoMapper roleMapper;

    //判断该角色存不存在
    @Override
    public boolean isRoleByName(String roleName) {
        QueryWrapper<RoleInfo> queryWrapper=new QueryWrapper<RoleInfo>();
        queryWrapper.eq("roleName", roleName);//判断该角色存不存在
        RoleInfo role=roleMapper.selectOne(queryWrapper);
        if(role == null) {//不存在
            return true;
        }else {
            return false;
        }
    }

    //添加
    @Override
    public boolean addRole(RoleInfo role) {
        boolean resultAdd=true;
        boolean result = isRoleByName(role.getRoleName());
        if(result) {//该角色能添加
            role.setDelFlag(0);
            roleMapper.insert(role);
            resultAdd = true;
        }else {
            resultAdd = false;
        }
        return resultAdd;
    }

    //修改
    @Override
    public boolean updateRole(RoleInfo role){
        boolean resultUpd=true;
        QueryWrapper<RoleInfo> queryWrapper=new QueryWrapper<RoleInfo>();
        queryWrapper.eq("roleName", role.getRoleName());
        queryWrapper.ne("roleId", role.getRoleId());

        RoleInfo roleOld =roleMapper.selectOne(queryWrapper);
        if(roleOld == null) {//该角色能修改
            roleMapper.updateById(role);
            resultUpd = true;
        }else {
            resultUpd = false;
        }
        return resultUpd;
    }

    @Override
    public List<RoleInfo> selectList(QueryWrapper<RoleInfo> queryWrapper) {
        return roleMapper.selectList(queryWrapper);
    }

    @Override
    public RoleInfo selectById(int roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public boolean updateById(RoleInfo role) {
        int updRole=roleMapper.updateById(role);
        if(updRole==0){
            return false;
        }else {
            return true;
        }
    }


}
